﻿using QLKDS.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLKDS.DAL
{
    public class BookStoreManager
    {
        public IList<BookStoreInfo> ListBookStoreByStoreId(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryBookStore = from d in db.BookStores
                                   join  c in db.Books on  d.BookId equals c.Id 
                                   join e in db.Stores on d.StoreId equals e.Id
                                    where e.Name.Equals(name)
                                       select new BookStoreInfo { Id = d.Id, BookId = d.BookId,BookName=c.Name,StoreName=e.Name, Publisher=c.Publisher, StoreId = d.StoreId,Quanlity=d.Quantity };

                return queryBookStore.ToList();
            }
        }

        public BookStore GetBookStore(int bookId, int storeId)
        {
            using (var db = new QLKDSDataContext())
            {
                var query = from d in db.BookStores
                            where d.BookId.Equals(bookId)
                            where d.StoreId.Equals(storeId)
                            select d;
                return query.First();
            }
        }

        public void CreateBookStore(BookStore BookStore)
        {
            using (var db = new QLKDSDataContext())
            {
                db.BookStores.InsertOnSubmit(BookStore);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="BookStore"></param>
        public void UpdateBookStore(BookStore BookStore)
        {
            using (var db = new QLKDSDataContext())
            {
                var _BookStore = db.BookStores.Single(d => d.Id.Equals(BookStore.Id));
                _BookStore.BookId = BookStore.BookId;
                _BookStore.Quantity = BookStore.Quantity;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteBookStore(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _BookStore = db.BookStores.Single(d => d.Id.Equals(id));

                db.BookStores.DeleteOnSubmit(_BookStore);
                db.SubmitChanges();
            }
        }
    }
}
